package com.fishing.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;

import org.springframework.util.StringUtils;
/*
 * 物理地址是48位，别和ipv6搞错了
 */
public class LOCALMAC {

//	public static void main(String[] args) throws UnknownHostException, SocketException {
//		getIpMask();
//	}
//	
	public static String getIpMask(String nets)  {
        String os = getOSName();
        String mac = "";
        System.out.println(os);
        if (os.startsWith("windows")) {
            // 本地是windows
            try {
				mac = getWindowsMACAddress();
			} catch (UnknownHostException e) {
				e.printStackTrace();
			} catch (SocketException e) {
				e.printStackTrace();
			}
        } else {
            // 本地是非windows系统 一般就是unix
            mac = getUnixMACAddress(nets);
        }
        System.out.println(mac);

        return mac;
    }

    public static String getOSName() {
        return System.getProperty("os.name").toLowerCase();
    }

    public static void main(String[] args) {
		String str = "74:D0:2B:C7:59:F8  TXQUEUELEN 1000  (ETHERNET)";
		System.out.println(str.substring(0,17));
	}
   
    private static String getUnixMACAddress(String nets) {
        String mac = null;
        BufferedReader bufferedReader = null;
        Process	process = null;
//        String [] netStrs = new String[]{"eth0","eth1","eth2","enp3s0","em1","em1:1"};
        
        String [] netStrs = nets.split(",");
        for (int i = 0; i < netStrs.length; i++) {
        	try {
            	process = Runtime.getRuntime().exec("ifconfig "+netStrs[i]);// linux下的命令，一般取eth0作为本地主网卡 显示信息中包含有mac地址信息
                bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
                String line = null;
                int index = -1;
                while ((line = bufferedReader.readLine()) != null) {
                	System.out.println(line.toString());
                    index = line.toLowerCase().indexOf("hwaddr");// 寻找标示字符串[hwaddr]
                    if (index >= 0) {// 找到了
                        mac = line.substring(index + "hwaddr".length() + 1).trim();// 取出mac地址并去除2边空格
                        break;
                    }else{
                    	index = line.toLowerCase().indexOf("ether");// 寻找标示字符串[hwaddr]
                    	if(index >= 0){
                    		mac = line.substring(index + "ether".length() + 1).trim();// 取出mac地址并去除2边空格
                    		mac = mac.substring(0,17);
                    		break;
                    	}
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (bufferedReader != null) {
                        bufferedReader.close();
                    }
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                bufferedReader = null;
                process = null;
            }
        	if(!StringUtils.isEmpty(mac)){
        		break;
        	}
		}
        System.out.println("mac:"+mac.toUpperCase());
        return mac.toUpperCase();
    }

   
    private static String getWindowsMACAddress() throws UnknownHostException, SocketException {
        InetAddress ia = InetAddress.getLocalHost();
        // 获取网卡，获取地址
        byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();
        // System.out.println("mac数组长度："+mac.length);
        StringBuffer sb = new StringBuffer("");
        for (int i = 0; i < mac.length; i++) {
            if (i != 0) {
                sb.append("-");
            }
            // 字节转换为整数
            int temp = mac[i] & 0xff;
            String str = Integer.toHexString(temp);
            // System.out.println("每8位:"+str);
            if (str.length() == 1) {
                sb.append("0" + str);
            } else {
                sb.append(str);
            }
        }
        System.out.println("本机MAC地址:" + sb.toString().toUpperCase());
        return sb.toString().toUpperCase();
    }
}
